REM >PCDirMenu
REM By Dave Lawrence 25Aug95
ON ERROR ERROR EXT ERR,REPORT$+" at line "+STR$ ERL:END
buf_size=1024
DIM buf buf_size
DIM f$(1000),d$(1000)
SYS "OS_GetEnv" TO env$

IF INSTR(env$,"-mouse") THEN
 *DirectorMouseDir X
 dir$=FNread("x")
ELSE
 dir$=FNarg(env$, "-dir", "")
ENDIF
IF dir$="" THEN INPUT "Directory ";dir$
ext$=FNarg(env$, "-ext", "")

option = FNswi("Director_Option")
command = FNswi("Director_Command")
dash = FNswi("Director_Dash")

SYS "OS_FSControl",37,dir$,buf,,,buf_size
dir$=FNstr0(buf)
OSCLI"DirectorParsePath "+dir$
SYS "Director_Menu","<Director$CurrentLeaf> PCMenu -temp"

n=0
files=0
dirs=0
REPEAT
 SYS "OS_GBPB",12,dir$,buf,100,n,1024,0 TO ,,,read,n
 at=buf
 WHILE read>0
  leaf$=FNstr0(at+24)
  CASE at!16 OF
   WHEN 1:
    d=INSTR(leaf$+"/","/")
    e$="."+MID$(leaf$,d+1)
    IF ext$="" THEN
     yes=FALSE
     FOR i=0 TO files-1
      IF f$(i)=e$ yes+=1
     NEXT
     IF yes=0 f$(files)=e$:files+=1
    ELSE
     IF ext$=e$ f$(files)=leaf$:files+=1
     REM ext provided
    ENDIF
   WHEN 2,3: IF ext$="" d$(dirs) =leaf$:dirs+=1
  ENDCASE
  read-=1
  at=at+(24+LEN leaf$+1+3)ANDNOT3
 ENDWHILE
UNTIL n=-1

IF dirs>0 THEN
 FOR i=0 TO dirs-1
  k=i
  FOR j=i TO dirs-1
   IF d$(j)<d$(k) k=j
  NEXT
  SWAP d$(i),d$(k)
  SYS option,d$(i)+" -sprite small_dir "+FNsub("-dir "+dir$+"."+d$(i))
 NEXT
ENDIF
IF files>0 THEN
 IF dirs>0 SYS dash
 FOR i=0 TO files-1
  k=i
  FOR j=i TO files-1
   IF f$(j)<f$(k) k=j
  NEXT
  SWAP f$(i),f$(k)
  IF ext$="" THEN
   SYS option,f$(i)+" "+FNsub("-dir "+dir$+" -ext "+f$(i))
  ELSE
   SYS option,LEFT$(f$(i),LEN f$(i)-LEN ext$)+ext$
   SYS command,"Filer_Run "+dir$+"."+f$(i)
  ENDIF
 NEXT
ENDIF
*EndMenu
*Set Director$Menu PCMenu
END
:
DEF FNsub(a$)
="-sub ""Dynamic:/Director:Menus.Files.PCDirMenu "+a$+""""
:
DEF FNf(f$)
dot=INSTR(f$,"/")
IF dot<LEN f$-4 THEN =f$
=MID$(f$+"   ",dot+1)+"/"+LEFT$(f$,dot-1)
:
DEF FNswi(name$)
SYS &39,,name$ TO swi
=swi

DEF FNread(a$)
?buf=13
SYS "XOS_ReadVarVal",a$,buf,buf_size,0,3 TO ,,read
buf?read=13
=$buf

DEF FNarg(str$,arg$,def$)
LOCAL i,j
i=INSTR(str$,arg$)
IF i=0 THEN =def$
i+=LEN arg$+1
j=INSTR(str$+" <"," ",i)
=MID$(str$,i,j-i)
:
DEF FNstr0(A)
A$=""
WHILE ?A<>0
 A$+=CHR$?A
 A+=1
ENDWHILE
=A$
